package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.nhc.model.NaviTrackSetInfo;
import com.nhc.model.PaomaoInfo;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.BottomMenuWindow;
import zuo.biao.library.ui.EditTextInfoWindow;
import zuo.biao.library.ui.TopMenuWindow;

public class PaomaoActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "PaomaoActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, PaomaoActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paomao_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private ScrollView svDemoMain;

    private TextView tv_range;
    private ImageView iv_swith;


    @Override
    public void initView() {//必须调用
        svDemoMain = (ScrollView) findViewById(R.id.svDemoMain);
        tv_range = (TextView) findViewById(R.id.tv_range);
        iv_swith = (ImageView) findViewById(R.id.iv_swith);
    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    PaomaoInfo _setInfo;

    @Override
    public void initData() {//必须调用
        _setInfo = PaomaoInfo.getInstance();
        _setInfo.load(this);

        refresh();
    }

    void refresh() {
        String DD[] = SysUtils.getInstance().getAltitudeStr(_setInfo.getPaomao_range());
        tv_range.setText(DD[0] + DD[1]);
        if (_setInfo.isPaomao_used()) {
            iv_swith.setImageResource(R.drawable.swith_open);
        } else {
            iv_swith.setImageResource(R.drawable.swith_close);
        }
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.ctrl_0).setOnClickListener(this);
        findViewById(R.id.ctrl_1).setOnClickListener(this);

    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        _setInfo.save(this);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应
        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu_paomao)), REQUEST_TO_TOP_MENU, false);
    }

    private int[] CTRL_IDS = {
            R.id.ctrl_0, R.id.ctrl_1};

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            default:
                break;
            case 21://left
            case 22://right
            case 19://up
            case 20://down
            case 23://ok
                setFoucseCtrl();
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    boolean _isFirst = true;

    private void setFoucseCtrl() {
        if (!_isFirst) {
            return;
        }

        _isFirst = false;

        for (int i = 0; i < CTRL_IDS.length; i++) {
            findViewById(CTRL_IDS[i]).setFocusable(true);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ctrl_0:
                onSetRange();
                break;
            case R.id.ctrl_1:
                onUsed();
                break;
            default:
                break;
        }
    }

    private void onUsed() {
        boolean isUsed = _setInfo.isPaomao_used();
        _setInfo.setPaomao_used(!isUsed);
        refresh();
    }

    private void onSetRange() {
        double range = _setInfo.getPaomao_range();
        String strUnit = "m";
        switch (SysUtils.getInstance().dis_mode) {
            default:
                break;
            case 4:
            case 1:
                range = range * 3.2808399;
                strUnit = "ft";
                break;
            case 2:
                range = range * 1.0936133;
                strUnit = "yd";
                break;
        }
        intent = EditNumberActivity.createIntentD(context
                , getString(R.string.xgfw), range, strUnit);
        toActivity(intent, REQUEST_TO_RANGE);
    }

    private final int REQUEST_TO_RANGE = 23;
    private final int REQUEST_TO_TOP_MENU = 30;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_RANGE:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditTextInfoWindow.RESULT_VALUE);
                    double range = Double.valueOf(editedValue);
                    switch (SysUtils.getInstance().dis_mode) {
                        default:
                            break;
                        case 1:
                        case 4:
                            //ft => m
                            range = range / 3.2808399;
                            break;
                        case 2: {
                            //yd => m
                            range = range / 1.0936133;
                        }
                        break;
                    }
                    _setInfo.setPaomao_range(range);
                    refresh();
                }
                break;
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            _setInfo.reset();
                            refresh();
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
    }


    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {

    }
    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
